/**
 * @Package
 */
package org.lanzier.common.utils.pretty;

/**
 * <p>
 * Title: SQLFormatter
 * <p>
 * Description: 为排版SQL易读的格式美化工具
 * <p>
 * SQL formats are not synchronized.
 * It is recommended to create separate format instances for each thread.
 * If multiple threads access a format concurrently, it must be synchronized
 * externally.
 * <p>
 * Copyright: Copyright (c) 2012
 * <p>
 * Company:fsti
 * <p>
 * 
 * @author liulf Dec 7, 2012
 * @version 1.0
 */
public abstract class SQLFormatter {

	public static SQLFormatter newFormatter(String sql) {
		String lowerCase = sql.trim().toLowerCase();
		if ( lowerCase.startsWith(DDLFormatter.LOWERCASE_CREATE_TABLE) 
				|| lowerCase.startsWith(DDLFormatter.LOWERCASE_ALTER_TABLE) 
				|| lowerCase.startsWith(DDLFormatter.LOWERCASE_COMMENT_ON) ) {
			return new DDLFormatter(sql);
		} else {
			return new QRYFormatter(sql);
		}
	}

	/**
	 * 格式化排版SQL
	 * @return
	 */
	public abstract String format();

	public static void main(String[] args) {
		if ( args.length>0 ) {
			StringBuffer buf = new StringBuffer( args.length * args[0].length() ).append( args[0] );
			for ( int i = 1; i < args.length; i++ ) {
				buf.append( " " ).append( args[i] );
			}
			SQLFormatter f = SQLFormatter.newFormatter(buf.toString());
			System.out.println( f.format() );
		}
	}

}
